$(function () {
	$("#jqGrid").jqGrid({
		url: baseURL + 'fun/emp/list',
		datatype: "json",
		colModel: [
			{ label: '员工ID', name: 'empId', index: "emp_id", width: 30, key: true},
			{ label: '姓名', name: 'empName',sortable: false, width: 100 },
			{ label: '身份证号', name: 'idCard', sortable: false, width: 100 },
			{ label: '所属部门', name: 'deptName', sortable: false, width: 100 },
			{ label: '职名（务）', name: 'office', sortable: false, width: 100 },
			{ label: '状态', name: 'status', sortable: false,width: 50, formatter: function(value, options, row){
					return value === 0 ?
						'<span >在岗</span>' :
						'<span >不在岗</span>';
				}},
			{ label: '备注', name: 'remarks', index: 'remarks', width: 100 }
		],
		viewrecords: true,
		height: 400,
		rowNum: 10,
		rowList : [10,30,50],
		rownumbers: true,
		rownumWidth: 25,
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader : {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames : {
			page:"page",
			rows:"limit",
			order: "order"
		},
		gridComplete:function(){
			//隐藏grid底部滚动条
			//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
			$("#jqGrid").setGridParam().hideCol("empId").trigger("reloadGrid");
		},
	});
});
//部门结构树
var dept_ztree;
var dept_setting = {
	data: {
		simpleData: {
			enable: true,
			idKey: "deptId",
			pIdKey: "parentId",
			rootPId: -1
		},
		key: {
			url:"nourl"
		}
	}
};

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			empName: null,
			deptId:null
		},
		showList: true,
		empId:null,
		emp:{},
		empAttachment:{},
		selectDept:'',
		works: [],
		rewards: [],
		pickerOptions: {
			disabledDate(time) {
				return time.getTime() > Date.now();
			}
		},
		politicalBackground: [{value: '群众'},{value:'团员'},{value:'预备党员'},{value:'党员'}],
		education: [{value:'大专'},{value:'本科'},{value:'硕士'},{value:'博士'}],
		statusType: [{value:'5',label:'不在岗'}],
		causes: [{value:'1',label:'请病假'},{value:'2',label:'停薪留职'}],
		fileList: [],
		timer: null,
        activeNames: ['1'],
		addEmp: {},
		changeEmpDept: [],
		changeEmpOffice: [],
		empStatusChange:[],
		statusChange: {},
		reverse: true,
		formData:[
				{index: 0,  isShow: false}, {index: 1,  isShow: false},
				{index: 2,  isShow: false}, {index: 3,  isShow: false},
				{index: 4,  isShow: false}, {index: 5,  isShow: false},
				{index: 6,  isShow: false}, {index: 7,  isShow: false},
				{index: 8,  isShow: false}, {index: 9,  isShow: false},
				{index: 10,  isShow: false}
			],
	},
	methods: {
		handleChange(val) {
			console.log(val);
		},
		query: function () {
			vm.reload();
		},
		empShow:function ()
		{
			vm.empId = getSelectedRow();
			if(vm.empId == null){
				return ;
			}
			vm.showList = false;
			vm.getInfo(vm.empId);
			/*this.getWork();
			this.getReward();*/
			vm.getAddEmp(vm.empId);
			vm.getChangeEmpDept(vm.empId);
			vm.getChangeEmpOffice(vm.empId);
			vm.getEmpStatusChange(vm.empId);
			vm.getStatusChange(vm.empId);
		},
		exportExcel: function (event) {
			var empIds = getSelectedRows();
			if(empIds == null){
				return ;
			}
			$.ajax({
				type: "POST",
				url: baseURL + "fun/emp/exportEmps",
				contentType: "application/json",
				responseType:'blob',
				data: JSON.stringify(empIds),
			}).then((res)=> {
				const link = document.createElement('a');
				let blob = new Blob([res.data]);
				link.style.display = 'none'
				link.href = URL.createObjectURL(blob);
				link.setAttribute('download', '测试')
				document.body.appendChild(link)
				link.click()
				document.body.removeChild(link);
			})
			alert('导出位置:G:\\empExcel');
		},
		getInfo: function(empId){
			vm.formData=[
				{index: 0,  isShow: false}, {index: 1,  isShow: false}, {index: 2,  isShow: false},
				{index: 3,  isShow: false}, {index: 4,  isShow: false}, {index: 5,  isShow: false},
				{index: 6,  isShow: false}, {index: 7,  isShow: false}, {index: 8,  isShow: false},
				{index: 9,  isShow: false}, {index: 10,  isShow: false}
			],
			vm.empAttachment={};
			$.get(baseURL + "fun/emp/info/"+empId, function(r){
				vm.emp = r.emp.empEntity;
				vm.selectDept=r.emp.deptParent;
				switch (vm.selectDept) {
					case 'jwd':
						vm.formData[0].isShow=true;
						vm.empAttachment= r.emp.jwdEmpEntity;
						console.log(vm.formData[0].isShow);
						break;
					case 'kyd':
						vm.formData[1].isShow=true;
						vm.empAttachment= r.emp.kydEmpEntity;
						break;
				}
				vm.getDept();
			});
		},
		getWork: function(empId){
			$.get(baseURL + "fun/emp/work/"+empId, function(r){
				vm.works = r.work;
			});
		},
		getReward: function(empId){
			$.get(baseURL + "fun/emp/reward/"+empId, function(r){
				vm.rewards = r.reward;
			});
		},
		getAddEmp:function(empId)
		{
			$.get(baseURL + "fun/emp/infoAddEmp/"+empId, function(r){
				vm.addEmp = r.addEmp;
			});
		},
		getChangeEmpDept:function(empId)
		{
			$.get(baseURL + "fun/emp/infoChangeEmpDept/"+empId, function(r){
				vm.changeEmpDept = r.changeEmpDept;
			});
		},
		getChangeEmpOffice:function(empId)
		{
			$.get(baseURL + "fun/emp/infoChangeEmpOffice/"+empId, function(r){
				vm.changeEmpOffice = r.changeEmpOffice;
			});
		},
		getStatusChange:function(empId)
		{
			$.get(baseURL + "fun/emp/infoStatusChange/"+empId, function(r){
				vm.statusChange = r.statusChange;
			});
		},
		getEmpStatusChange:function(empId)
		{
			$.get(baseURL + "fun/emp/infoEmpStatusChange/"+empId, function(r){
				vm.empStatusChange = r.empStatusChange;
			});
		},
		getDept: function(){
			//加载部门树
			$.get(baseURL + "sys/dept/list", function(r){
				dept_ztree = $.fn.zTree.init($("#deptTree"), dept_setting , r);
				var node = dept_ztree.getNodeByParam("deptId", vm.emp.deptId);
				if(node != null){
					dept_ztree.selectNode(node);
					vm.emp.deptName = node.name;
				}
			})
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'name': vm.q.empName,'deptId':vm.q.deptId},
				page:page
			}).trigger("reloadGrid");
		},

	}
});
window.addEventListener("message",function(event){

	vm.showList = true;
	vm.q.deptId=event.data;
	var page = $("#jqGrid").jqGrid('getGridParam','page');
	$("#jqGrid").jqGrid('setGridParam',{
		postData:{'deptId':event.data,'name': ''},
		page:page
	}).trigger("reloadGrid");
}, false);